﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Client.Wpf.Services;
using AgileTimeTracker.Domain.TaskModule;
using Microsoft.Win32;

namespace AgileTimeTracker.Client.Wpf.TimeControlModule
{
    public class WorkViewModel : PageViewModel
    {
        #region Fields

        private double elapsedSeconds;
        private Timer timer;
        private TaskWorkTimeItemViewModel workingTask;
        private bool hasTaskStarted;
        private string workingTaskTitle;
        private bool isStartTaskVisible;
        private bool isStopTaskVisible;
        private TaskListViewModel taskViewModel;
        private TaskListViewModel realTaskViewModel;


        #endregion // Fields

        public WorkViewModel()
        {
            this.Title = "Work";
            this.timer = new Timer(1000);
            this.timer.Elapsed += TimeElapsed;
            this.realTaskViewModel = new TaskListViewModel();
            this.taskViewModel = this.realTaskViewModel;
            this.GlueBusyInfo(this.realTaskViewModel);
            this.IsStartTaskVisible = true;
            SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
        }

        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            this.StopTask();
        }

        #region Properties

        public bool IsStartTaskVisible
        {
            get { return this.isStartTaskVisible; }
            set { this.OnPropertyChanged(ref this.isStartTaskVisible, value); }
        }

        public bool IsStopTaskVisible
        {
            get { return this.isStopTaskVisible; }
            set { this.OnPropertyChanged(ref this.isStopTaskVisible, value); }
        }

        public string WorkingTaskTitle
        {
            get { return this.workingTaskTitle; }
            set { this.OnPropertyChanged(ref this.workingTaskTitle, value); }
        }

        public TaskListViewModel TasksViewModel
        {
            get { return this.taskViewModel; }
            set { this.OnPropertyChanged(ref this.taskViewModel, value); }
        }

        public bool HasTaskStarted
        {
            get { return this.hasTaskStarted; }
            set { this.OnPropertyChanged(ref this.hasTaskStarted, value); }
        }

        public double ElapsedSeconds
        {
            get { return this.elapsedSeconds; }
            set { this.OnPropertyChanged(ref this.elapsedSeconds, value); }
        }

        #endregion // Properties

        #region Commands

        public RelayCommand StartTaskCommand { get; set; }

        public void StartTask()
        {
            this.StopTask();
            this.timer.Start();
            this.HasTaskStarted = true;
            this.workingTask = this.TasksViewModel.SelectedTask;
            this.WorkingTaskTitle = string.Format("{0} - {1} - {2}", this.workingTask.TfsWorkItemId.GetValueOrDefault(), this.workingTask.UserStory, this.workingTask.Title);
            this.IsStopTaskVisible = true;
            this.IsStartTaskVisible = false;
        }

        public void ToogleTaskList()
        {
            if (this.taskViewModel == null)
            {
                this.TasksViewModel = this.realTaskViewModel;
            }
            else
            {
                this.TasksViewModel = null;
            }
        }

        public RelayCommand StopTaskCommand { get; set; }

        public void StopTask()
        {
            var now = DateTime.Now;
            var taskDuration = this.elapsedSeconds;
            this.timer.Stop();
            this.HasTaskStarted = false;
            if (this.workingTask == null)
            {
                return;
            }

            var workTime = new TfsTaskWorkTime();
            workTime.StartDate = now.AddSeconds(-taskDuration);
            workTime.EndDate = now;
            this.workingTask.CurrentTask.WorkTimes.Add(workTime);
            AppService.Instance.SaveUser(MainWindowViewModel.Instance.CurrentUser);            
            this.realTaskViewModel.RefreshTasks();                        
            this.ElapsedSeconds = 0;
            this.WorkingTaskTitle = string.Empty;
            this.IsStopTaskVisible = false;
            this.IsStartTaskVisible = true;
        }

        #endregion // Commands

        #region Initialize

        protected override void Close()
        {
            this.StopTask();
            this.timer.Dispose();
            this.timer = null;
            this.UnglueBusyInfo(this.realTaskViewModel);
            this.realTaskViewModel = null;
            this.TasksViewModel = null;
            base.Close();
        }

        protected override void InititalizeCommands()
        {
            base.InititalizeCommands();
            this.StartTaskCommand = new RelayCommand(this.StartTask);
            this.StopTaskCommand = new RelayCommand(this.StopTask);
        }

        #endregion // Initialize

        #region Event Handles

        private void TimeElapsed(object sender, ElapsedEventArgs e)
        {
            this.ElapsedSeconds += 1.0;
        }

        #endregion // Event Handlers
    }
}
